package com.ly.ThreadExit;

public class ThreadMethod05 {
    public static void main(String[] args) throws InterruptedException {
        MyDaemonThread myDaemonThread = new MyDaemonThread();
        myDaemonThread.setDaemon(true);
        myDaemonThread.start();
        //如果希望当主线程结束后，子线程可以自动结束
        //只需将子线程，设为守护线程即可

        for (int i = 0; i < 10; i++) {
            System.out.println("工作中");
            Thread.sleep(1000);
        }
    }
}
class MyDaemonThread extends Thread {
    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("线程执行了");
        }
    }
}
